from ocms.eventmanager.models import *
from django.contrib.auth.models import *
from django.core.mail import EmailMessage
from django.template import Context, loader, RequestContext
from django.conf import settings
from django.utils.encoding import smart_str, smart_unicode
from django.db.models import Q
import datetime

def getGCCMembers():
    return User.objects.filter(groups__name="Global Conferences Committee")

def has_voting_rights(user):
    if user in getGCCMembers():
        return True
    
    return False

def sendEmail(event, action):
    planner_names = None
    planner_emails = []
    for planner in event.planning_team.all():
        planner_names = smart_str(planner.first_name) + ", "
        planner_emails.append(smart_str(planner.email))
    
    #planner_emails.append(settings.GCC_EMAIL)
    
    if (action=="approve"):
        subject="[OEMS] Approval of "+smart_str(event.Name)
        planner_emails.append(settings.GCC_LIST)
        template = 'events/approve_email.txt'
    elif (action=="deny"):
        subject="[OEMS] Denial of "+smart_str(event.Name)
        template = 'events/deny_email.txt'
    elif (action=="new"):
        planner_emails.append(settings.GCC_LIST)
        subject="[OEMS] ACTION REQUIRED: New \""+smart_str(event.Name)+"\" event application"
        template = 'events/new_email.txt'
    elif (action=="hold"):
        planner_emails.append(settings.GCC_EMAIL)
        subject="[OEMS] "+smart_str(event.Name)+" has been placed on hold"
        template = 'events/hold_email.txt'
    elif (action=="unhold"):
        planner_emails.append(settings.GCC_EMAIL)
        subject="[OEMS] Hold removed on "+smart_str(event.Name)
        template = 'events/unhold_email.txt'
    
    context = Context({
            'event_name': event.Name,
            'event_dates': event.strDates(),
            'event_txt': event.cast().TxtContent(),
            'planner_names': planner_names,
            })
    t = loader.get_template(template)
    body = t.render(context)

    msg = EmailMessage(subject, body, 'OWASP Event Management System <oems@owasp.org>',planner_emails)
    msg.send()
    
def active_planner_emails_sql():
    from django.db import connection, transaction
    cursor = connection.cursor()
    cursor.execute("Select distinct(email) from auth_user, eventmanager_event,  eventmanager_event_planning_team where eventmanager_event.event_status='A' AND auth_user.id = eventmanager_event_planning_team.user_id AND eventmanager_event_planning_team.event_id = eventmanager_event.id") 
    return cursor.fetchall() 

def get_active_under_review_events():
    from ocms.eventmanager.models import Event
    return Event.objects.filter(event_status='A').filter(Q(approval_status='U') | Q(approval_status='H')).order_by('date_created')

def get_my_active_under_review_events(user):
    from ocms.eventmanager.models import Event
    if has_voting_rights(user) or user.is_staff or user.is_superuser:
        #superusers get all active events
        return Event.objects.filter(event_status='A').filter(Q(approval_status='U') | Q(approval_status='H')).order_by('date_created')
    else:
          #non superusers get all events for which they are on the planning team
        return Event.objects.filter(Q(approval_status='U') | Q(approval_status='H')).filter(event_status='A', planning_team=user).order_by('Start_Date')
